<template>
  <span ref="target" class="ts-editor-resizer">
    <slot></slot>
  </span>
</template>

<script>
export default {
  name: 'Resizer',
  props: {
    type: {
      type: String
    },
    minWidth: {
      type: Number,
      default: 260
    },
    maxWidth: {
      type: Number,
      default: 600
    }
  },
  data() {
    return {
    }
  },
  mounted() {
    const _this = this
    let _el = this.$refs.target
    _el.onmousedown = function (e) {
      var init = e.clientX
      var initWidth =
        _this.type == 'right'
          ? _this.$parent.columnWidth.right
          : _this.$parent.columnWidth.left
      document.onmousemove = function (e) {
        var end = e.clientX
        var newWidth =
          _this.type == 'right'
            ? init - end + initWidth
            : end - init + initWidth
        if (newWidth < _this.minWidth) { // 最小拖动300
          return
        }
        if (newWidth > _this.maxWidth) { // 最大拖动800
          return
        }
        if (_this.type == 'right') {
          _this.$parent.columnWidth.right = newWidth
        } else {
          _this.$parent.columnWidth.left = newWidth
        }
        _this.$parent.columnWidth.center = `calc(100% - ${_this.$parent
          .columnWidth.right + _this.$parent.columnWidth.left}px)`// 获取中间位置
      }
      document.onmouseup = function () {
        document.onmousemove = document.onmouseup = null
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.ts-editor-resizer {
  border-left: 1px solid transparent;
  border-right: 1px solid transparent;
  width:4px;
  margin: 0 -5px;
  height: 100%;
  opacity: 0.9;
  background: padding-box #d8dee8;
  box-sizing: border-box;
  cursor: col-resize;
  z-index: 1;

  &:hover {
    border-color: #d8dee8;
  }

  .icon-container {
    visibility: hidden;
    opacity: 0;
    transition: opacity 0.4s;
    width: 20px;
    height: 120px;
    line-height: 120px;
    text-align: center;
    background: #d8dee8;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    cursor: pointer;
    &.position-left {
      transform: translate(calc(-100% - 4px), -50%);
    }
    &.position-right {
      transform: translate(calc(100% + 4px), -50%);
    }
  }
  .icon {
    color: #fff;
    font-size: 18px;
  }
}

.magic-editor-resizer:hover {
  .icon-container {
    visibility: visible;
    opacity: 1;
  }
  // border-left: 5px solid rgba(0,0,0,.5);
  // border-right: 5px solid rgba(0,0,0,.5);
}
</style>
